package com.example.cheeseerpadmin.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusInport;
import com.example.cheeseerpadmin.bus.entity.BusProvider;
import com.example.cheeseerpadmin.bus.service.BusGoodsService;
import com.example.cheeseerpadmin.bus.service.BusInportService;
import com.example.cheeseerpadmin.bus.service.BusProviderService;
import com.example.cheeseerpadmin.bus.vo.InportVo;
import com.example.cheeseerpadmin.sys.common.Constant;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import com.example.cheeseerpadmin.sys.common.WebUtils;
import com.example.cheeseerpadmin.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cheese
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/inport")
public class InportController {
    @Autowired
    public BusInportService busInportService;
    @Autowired
    public BusProviderService busProviderService;
    @Autowired
    public BusGoodsService busGoodsService;

    /**
     * 加载全部进货数据
     *
     * @param inportVo
     * @return
     */
    @RequestMapping("/loadAllInport")
    public DataGridView loadAllInport(InportVo inportVo) {
        Page<BusInport> busInportPage = new Page<>();
        QueryWrapper<BusInport> busInportQueryWrapper = new QueryWrapper<>();
        busInportQueryWrapper.eq(inportVo.getProviderid() != null && inportVo.getProviderid() != 0, "providerid", inportVo.getProviderid());
        busInportQueryWrapper.eq(inportVo.getGoodsid() != null && inportVo.getGoodsid() != 0, "goodsid", inportVo.getGoodsid());
        busInportQueryWrapper.ge(inportVo.getStartDate() != null, "inporttime", inportVo.getStartDate());
        busInportQueryWrapper.le(inportVo.getEndDate() != null, "inporttime", inportVo.getEndDate());
        busInportQueryWrapper.like(StringUtils.isNotBlank(inportVo.getOperateperson()), "operateperson", inportVo.getOperateperson());
        busInportQueryWrapper.like(StringUtils.isNotBlank(inportVo.getRemark()), "remark", inportVo.getRemark());
        busInportQueryWrapper.orderByDesc("inporttime");
        Page<BusInport> page = busInportService.page(busInportPage, busInportQueryWrapper);
        List<BusInport> records = page.getRecords();
        for (BusInport busInport : records) {
            Integer providerid = busInport.getProviderid();
            Integer goodsid = busInport.getGoodsid();
            BusProvider busProvider = busProviderService.getById(providerid);
            BusGoods busGoods = busGoodsService.getById(goodsid);
            if (busProvider != null) {
                busInport.setProvidername(busProvider.getProvidername());
            }

            if (busGoods != null) {
                busInport.setGoodsname(busGoods.getGoodsname());
                busInport.setSize(busGoods.getSize());
            }
        }
        return new DataGridView((long) records.size(), records);
    }

    /**
     * 加载所有供应商
     *
     * @return
     */
    @RequestMapping("/loadAllProviderForSelect")
    public DataGridView loadAllProviderForSelect() {
        QueryWrapper<BusProvider> busProviderQueryWrapper = new QueryWrapper<>();
        busProviderQueryWrapper.eq("available", Constant.AVAILABLE_TRUE);
        List<BusProvider> list = busProviderService.list(busProviderQueryWrapper);
        return new DataGridView(list);
    }

    /**
     * 根据供应商加载商品名称
     *
     * @param id
     * @return
     */
    @RequestMapping("/loadGoodsByProviderId")
    public DataGridView loadGoodsByProviderId(Integer id) {
        QueryWrapper<BusGoods> busGoodsQueryWrapper = new QueryWrapper<>();
        busGoodsQueryWrapper.eq("available", Constant.AVAILABLE_TRUE);
        busGoodsQueryWrapper.eq(id != null && id != 0, "providerid", id);
        List<BusGoods> list = busGoodsService.list(busGoodsQueryWrapper);
        return new DataGridView(list);
    }

    /**
     * 添加商品进货信息
     *
     * @param busInport
     * @return
     */
    @RequestMapping("/addInport")
    public ResultObj addInport(BusInport busInport) {
        try {
            busInport.setInporttime(new Date());
            SysUser user = (SysUser) WebUtils.getSession().getAttribute("user");
            busInport.setOperateperson(user.getName());
            busInport.setBalance(busInport.getNumber());
            busInportService.save(busInport);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     *
     * @param busInport
     * @return
     */
    @RequestMapping("/updateInport")
    public ResultObj updateInport(BusInport busInport) {
        try {
            busInportService.updateById(busInport);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除进货信息
     * @param id
     * @return
     */
    @RequestMapping("/deleteInportById")
    public ResultObj deleteInportById(Integer id) {
        try {
            busInportService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

